//
//  UILabel+ZSSetText.m
//  Epayment
//
//  Created by safiri on 2018/6/13.
//  Copyright © 2018年 safiri. All rights reserved.
//

#import "UILabel+ZSSetText.h"

@implementation UILabel (ZSSetText)

- (void)setAttributedTextWithHTMLString:(NSString *)htmlStr {
    NSAttributedString *textAttr = [[NSAttributedString alloc]initWithData:[htmlStr dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
    
    self.attributedText = textAttr;
}
- (void)setMutableAttrTextWithHTMLString:(NSString *)htmlStr documentAttributes:(NSDictionary <NSString *, id> *)dict{
    if (!dict) {
        dict = @{NSFontAttributeName:[UIFont systemFontOfSize:14]};
    }
    NSMutableAttributedString *textAttr = [[NSMutableAttributedString alloc] initWithData:[htmlStr dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
    [textAttr addAttributes:dict range:NSMakeRange(0, textAttr.length)];
    self.attributedText = textAttr;
}
+ (CGFloat)getHeightTWithHTMLAttributedString:(NSString *)htmlStr width:(CGFloat)width{
    NSAttributedString *textAttr = [[NSAttributedString alloc]initWithData:[htmlStr dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
    CGRect rect = [textAttr boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];
    return ceilf(rect.size.height);
}

@end
